---
title: "Reference: MCPServer | Exposing Kastrax Tools via MCP | Kastrax Docs"
description: API Reference for MCPServer - A class for exposing Kastrax tools and capabilities as a Model Context Protocol server.
---

# MCPServer ✅

The `MCPServer` class provides the functionality to expose your existing Kastrax tools as a Model Context Protocol (MCP) server. This allows any MCP client (like Cursor, Windsurf, or Claude Desktop) to connect to the tools and make them available to an agent.
Note that if you only need to use your tools in your Kastrax agents you don't need to create an MCP server. This API allows you to expose your Kastrax tools to external MCP clients. In Kastrax you can use your tools directly without MCP.

It supports both [stdio (subprocess) and SSE (HTTP) MCP transports](https://modelcontextprotocol.io/docs/concepts/transports).

## Properties ✅

To create a new MCPServer, you need to provide some basic information about your server and the tools it will offer.

<PropertiesTable
  content={[
    {
      name: "name",
      type: "string",
      description: "A name for your server (like 'My Weather Server').",
    },
    {
      name: "version",
      type: "string",
      description: "The version of your server (like '1.0.0').",
    },
    {
      name: "tools",
      type: "ToolsInput",
      description:
        "An object containing the tools you want to make available. This can include tools created with Kastrax or the Vercel AI SDK.",
    },
  ]}
/>

For example, here's how you might create a new `MCPServer` instance:

```typescript
import { MCPServer } from "@kastrax/mcp";
import { weatherTool } from "./tools"; // Assuming you have a weather tool defined in this file

const server = new MCPServer({
  name: "My Weather Server",
  version: "1.0.0",
  tools: { weatherTool },
});
```

## Methods ✅

These are the functions you can call on an `MCPServer` instance to control its behavior and get information.

### startStdio()

Use this method to start the server so it communicates using standard input and output (stdio). This is typical when running the server as a command-line program.

```typescript
async startStdio(): Promise<void>
```

Here's how you would start the server using stdio:

```typescript
const server = new MCPServer({
  // example configuration above
});
await server.startStdio();
```

### startSSE()

This method helps you integrate the MCP server with an existing web server to use Server-Sent Events (SSE) for communication. You'll call this from your web server's code when it receives a request for the SSE or message paths.

```typescript
async startSSE({
  url,
  ssePath,
  messagePath,
  req,
  res,
}: {
  url: URL;
  ssePath: string;
  messagePath: string;
  req: any;
  res: any;
}): Promise<void>
```

Here's an example of how you might use `startSSE` within an HTTP server request handler. In this example an MCP client could connect to your MCP server at `http://localhost:1234/sse`:

```typescript
import http from "http";

const httpServer = http.createServer(async (req, res) => {
  await server.startSSE({
    url: new URL(req.url || "", `http://localhost:1234`),
    ssePath: "/sse",
    messagePath: "/message",
    req,
    res,
  });
});

httpServer.listen(PORT, () => {
  console.log(`HTTP server listening on port ${PORT}`);
});
```

Here are the details for the values needed by the `startSSE` method:

<PropertiesTable
  content={[
    {
      name: "url",
      type: "URL",
      description: "The web address the user is requesting.",
    },
    {
      name: "ssePath",
      type: "string",
      description: "The specific part of the URL where clients will connect for SSE (e.g., '/sse').",
    },
    {
      name: "messagePath",
      type: "string",
      description: "The specific part of the URL where clients will send messages (e.g., '/message').",
    },
    {
      name: "req",
      type: "any",
      description: "The incoming request object from your web server.",
    },
    {
      name: "res",
      type: "any",
      description: "The response object from your web server, used to send data back.",
    },
  ]}
/>

### getStdioTransport()

If you started the server with `startStdio()`, you can use this to get the object that manages the stdio communication. This is mostly for checking things internally or for testing.

```typescript
getStdioTransport(): StdioServerTransport | undefined
```

### getSseTransport()

If you started the server with `startSSE()`, you can use this to get the object that manages the SSE communication. Like `getStdioTransport`, this is mainly for internal checks or testing.

```typescript
getSseTransport(): SSEServerTransport | undefined
```

### tools()

This method gives you a look at the tools that were set up when you created the server. It's a read-only list, useful for debugging purposes.

```typescript
tools(): Readonly<Record<string, ConvertedTool>>
```

## Examples ✅

For practical examples of setting up and deploying an MCPServer, see the [Deploying an MCPServer Example](/examples/agents/deploying-mcp-server).

## Related Information ✅

- For connecting to MCP servers in Kastrax, see the [MCPClient documentation](./mcp-client).
- For more about the Model Context Protocol, see the [@modelcontextprotocol/sdk documentation](https://github.com/modelcontextprotocol/typescript-sdk).
